| Conditions | 3 |
| Paths | 4 |
| Total Lines | 94 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['leaflet', 'rbush', 'helper'], |
||
| 89 | prepareLabels: function () { |
||
| 90 | var d = this.data; |
||
| 91 | |||
| 92 | // label: |
||
| 93 | // - position (WGS84 coords) |
||
| 94 | // - offset (2D vector in pixels) |
||
| 95 | // - anchor (tuple, textAlignment, textBaseline) |
||
| 96 | // - minZoom (inclusive) |
||
| 97 | // - label (string) |
||
| 98 | // - color (string) |
||
| 99 | |||
| 100 | var labelsOnline = d.online.map(prepareLabel(null, 11, 8, true)); |
||
| 101 | var labelsOffline = d.offline.map(prepareLabel('rgba(212, 62, 42, 0.9)', 9, 5, false)); |
||
| 102 | var labelsNew = d.new.map(prepareLabel('rgba(48, 99, 20, 0.9)', 11, 8, true)); |
||
| 103 | var labelsLost = d.lost.map(prepareLabel('rgba(212, 62, 42, 0.9)', 11, 8, true)); |
||
| 104 | |||
| 105 | var labels = [] |
||
| 106 | .concat(labelsNew) |
||
| 107 | .concat(labelsLost) |
||
| 108 | .concat(labelsOnline) |
||
| 109 | .concat(labelsOffline); |
||
| 110 | |||
| 111 | var minZoom = this.options.minZoom; |
||
| 112 | var maxZoom = this.options.maxZoom; |
||
| 113 | |||
| 114 | var trees = []; |
||
| 115 | |||
| 116 | var map = this._map; |
||
| 117 | |||
| 118 | function nodeToRect(z) { |
||
| 119 | return function (n) { |
||
| 120 | var p = map.project(n.position, z); |
||
| 121 | return { minX: p.x - nodeRadius, minY: p.y - nodeRadius, maxX: p.x + nodeRadius, maxY: p.y + nodeRadius }; |
||
| 122 | }; |
||
| 123 | } |
||
| 124 | |||
| 125 | for (var z = minZoom; z <= maxZoom; z++) { |
||
| 126 | trees[z] = rbush(9); |
||
| 127 | trees[z].load(labels.map(nodeToRect(z))); |
||
| 128 | } |
||
| 129 | |||
| 130 | labels = labels.map(function (n) { |
||
| 131 | var best = labelLocations.map(function (loc) { |
||
| 132 | var offset = calcOffset(n.offset, loc); |
||
| 133 | var i; |
||
| 134 | |||
| 135 | for (i = maxZoom; i >= minZoom; i--) { |
||
| 136 | var p = map.project(n.position, i); |
||
| 137 | var rect = labelRect(p, offset, loc, n, minZoom, maxZoom, i); |
||
| 138 | var candidates = trees[i].search(rect); |
||
| 139 | |||
| 140 | if (candidates.length > 0) { |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return { loc: loc, z: i + 1 }; |
||
| 146 | }).filter(function (k) { |
||
| 147 | return k.z <= maxZoom; |
||
| 148 | }).sort(function (a, b) { |
||
| 149 | return a.z - b.z; |
||
| 150 | })[0]; |
||
| 151 | |||
| 152 | if (best !== undefined) { |
||
| 153 | n.offset = calcOffset(n.offset, best.loc); |
||
| 154 | n.minZoom = best.z; |
||
| 155 | n.anchor = best.loc; |
||
| 156 | |||
| 157 | for (var i = maxZoom; i >= best.z; i--) { |
||
| 158 | var p = map.project(n.position, i); |
||
| 159 | var rect = labelRect(p, n.offset, best.loc, n, minZoom, maxZoom, i); |
||
| 160 | trees[i].insert(rect); |
||
| 161 | } |
||
| 162 | |||
| 163 | return n; |
||
| 164 | } |
||
| 165 | return undefined; |
||
| 166 | }).filter(function (n) { |
||
| 167 | return n !== undefined; |
||
| 168 | }); |
||
| 169 | |||
| 170 | this.margin = 16; |
||
| 171 | |||
| 172 | if (labels.length > 0) { |
||
| 173 | this.margin += labels.map(function (n) { |
||
| 174 | return n.width; |
||
| 175 | }).sort().reverse()[0]; |
||
| 176 | } |
||
| 177 | |||
| 178 | this.labels = rbush(9); |
||
| 179 | this.labels.load(labels.map(mapRTree)); |
||
| 180 | |||
| 181 | this.redraw(); |
||
| 182 | }, |
||
| 183 | drawTile: function (canvas, tilePoint, zoom) { |
||
| 230 |